package fileReader.demo.reader;

import java.io.DataInputStream;
import java.io.IOException;

public class PointReader extends LayerReader {

	/**
	 * Method reads rock specific data from main file (.shp)
	 * For more information read ESRI Technical Dscription 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param fileLength length of file to read
	 * @author Mari
	 */
	public void readPointData(DataInputStream dataIn, String layer, MapLayer mapLayer, int fileLength) {
		try { 
			int i = 100;									// mainheader on jo luettu
			while(i<fileLength)
			{
				dataIn.readInt();				//rec header
				int contLength = 2*(dataIn.readInt());		//rec header
				i = i + 8;
				
				readLittleInteger(dataIn.readInt());	//shapetype	
				double x = readLittleDouble(dataIn.readDouble());		// x cordinate
				double y = readLittleDouble(dataIn.readDouble());		// y cordinate
				
				if(x>mapLayer.getScale_factor())
					mapLayer.setScale_factor(x);
			
				if(y>mapLayer.getScale_factor())
					mapLayer.setScale_factor(y);
				
				if(layer.contentEquals("rock")){
					createRock(x, y, mapLayer);
				}
				else if(layer.contentEquals("limitp")){
					createLimitP(x, y, mapLayer);
				}
				else if(layer.contentEquals("obstruct")){
					createObstruct(x, y, mapLayer);
				}
				else if(layer.contentEquals("harbour")){
					createHarbour(x, y, mapLayer);
				}
				else if(layer.contentEquals("natseabed")){
					createNatSeaBed(x, y, mapLayer);
				}
				else if(layer.contentEquals("text")){
					createText(x, y, mapLayer);
				}
				
				i = i + contLength;
			}
		} 
		catch (IOException e) 
		{
			readDataError();
			System.out.println("pointreader" + layer);
		}
		
	}
	
	private void createRock(double x, double y, MapLayer mapLayer){
		PointDrawer rocks = new PointDrawer();
		rocks.setX(x);
		rocks.setY(y);
		mapLayer.data.add(rocks);
	}
	
	private void createLimitP(double x, double y, MapLayer mapLayer) {
		PointDrawer limitP = new PointDrawer();
		limitP.setX(x);
		limitP.setY(y);
		mapLayer.data.add(limitP);
	}
	
	private void createObstruct(double x, double y, MapLayer mapLayer) {
		PointDrawer obstruct = new PointDrawer();
		obstruct.setX(x);
		obstruct.setY(y);
		mapLayer.data.add(obstruct);
	}

	private void createHarbour(double x, double y, MapLayer mapLayer) {
		PointDrawer harbour = new PointDrawer();
		harbour.setX(x);
		harbour.setY(y);
		mapLayer.data.add(harbour);
	}
	
	private void createNatSeaBed(double x, double y, MapLayer mapLayer) {
		PointDrawer seabed = new PointDrawer();
		seabed.setX(x);
		seabed.setY(y);
		mapLayer.data.add(seabed);
	}
	
	private void createText(double x, double y, MapLayer mapLayer) {
		PointDrawer text = new PointDrawer();
		text.setX(x);
		text.setY(y);
		mapLayer.data.add(text);
	}
	
	/**
	 * Method reads rock specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readRockAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
		for(int n=0; n<numOfRecords; n++)
		{
			char value = (char)dataIn.readByte(); //record deleted flag
			if(value == 42)
			{
				recordDeleted();
			}	
			
			skipLayerBytes(dataIn);
			
			dataIn.skipBytes(11);	// FI_RELROCK
			
			dataIn.skipBytes(32);	// NAMEROCK
			
			dataIn.skipBytes(11);	// FI_TYPROCK
		
			String depthString = "";	//DEPTH
			boolean numeric = true;
			for(int m=0; m<5; m++)
			{
				depthString = depthString + (char)dataIn.readByte();
				if(!Character.isDigit(depthString.charAt(m)))
					numeric  = false;
			}
			
			if(numeric)
			{
				double depth = Double.parseDouble(depthString);
				((PointDrawer) layer.data.get(n)).setDepth(depth);
			}
	
		}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}


	/**
	 * Method reads limitp specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readLimitPAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
							
				String limit = "";
				for(int m=0; m<32; m++)
				{
					limit = limit + (char)dataIn.readByte();
				}
				
				String limit2 = "";
				for(int m=0; m<128; m++)
				{
					limit2 = limit2 + (char)dataIn.readByte();
				}
				
				String typeLimit = "";	// TYPELIMIT
				for(int m=0; m<11; m++)
				{
					typeLimit = typeLimit + (char)dataIn.readByte();
				}
				((PointDrawer) layer.data.get(n)).setTypeLimit(typeLimit);
				
			}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}
	

	/**
	 * Method reads obstruct specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Tero Maijala
	 */
	public void readObstructAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				String depObs = "";	//DEPOBSTRN
				for(int m=0; m<5; m++)
				{
					depObs = depObs + (char)dataIn.readByte();
				}
				
				String typeObs = "";	//TYPEOBSTRN
				for(int m=0; m<11; m++)
				{
					typeObs = typeObs + (char)dataIn.readByte();
				}
				((PointDrawer) layer.data.get(n)).setTypeObstruct(typeObs);
					 
			}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}

	

	/**
	 * Method reads harbour specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Tero Maijala
	 */
	public void readHarbourAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(32);	// INT_NAME
				
				dataIn.skipBytes(11);	// NUMHARBOR
				
				dataIn.skipBytes(32);	// FI_OWNER
				
				dataIn.skipBytes(11);	// TYPEHARBOR
				
				String nameHarbor = "";	// NAMEHARBOR
				for(int m=0; m<32; m++)
				{
					nameHarbor = nameHarbor + (char)dataIn.readByte();
				}
				((PointDrawer) layer.data.get(n)).setHarborName(nameHarbor);
			}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}
	

	/**
	 * Method reads natseabed specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readNatSeaBedAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				//These two has only stars in oulu area map
				dataIn.skipBytes(11);	// QUASEABED
				dataIn.skipBytes(11);	// TYPEINVEST
							
				String typeSeaBed = "";	//TYPESEABED
				for(int m=0; m<11; m++)
				{
					typeSeaBed = typeSeaBed + (char)dataIn.readByte();
				}
				
				((PointDrawer) layer.data.get(n)).setTypeSeaBed(typeSeaBed);
					
				//this has also only stars in oulu area map	
				dataIn.skipBytes(11);	// COLSEABED
						
			}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}


	/**
	 * Method reads text specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readTextAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(64);	// TXSTR
				
				String ntxstr = "";	
				for(int m=0; m<64; m++)
				{
					ntxstr = ntxstr + (char)dataIn.readByte();
				
				}
				
				((PointDrawer) layer.data.get(n)).setNtxstr(ntxstr);
			
				
				dataIn.skipBytes(18);	// FI_ORIGLO
				
				dataIn.skipBytes(17);	// FOBJID
				
				dataIn.skipBytes(11);	// TYPETXT
			
				dataIn.skipBytes(18);	// FI_ORIGLA
				
				dataIn.skipBytes(12);	// OBJCLSNAME
				
			}
		
		}catch (IOException e) {
			readDataError();
			System.out.println("pointreader" + layer);
		}
	}

}
